package com.apitest.utils;


import com.apitest.config.Parameters;
import com.apitest.moudle.Attributes;
import io.restassured.RestAssured;
import io.restassured.config.SSLConfig;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import com.apitest.moudle.ContentType;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static io.restassured.RestAssured.given;

public class ApiKit {

    /**
     * 请求综合方法
     *
     * @param method  请求方式
     * @param apiPath 请求路径
     * @param cookies cookie Map
     * @param headers header Map
     * @param params  参数 Map
     * @param body    Body String
     * @return response
     */
    public static Response launch(String method,
                                  String apiPath,
                                  Map<String, Object> cookies,
                                  Map<String, Object> headers,
                                  Map<String, Object> params,
                                  String body) {
        RequestSpecification request = given().contentType(Parameters.getContentType().getType()).config((RestAssured.config().sslConfig(new SSLConfig().relaxedHTTPSValidation())));
        apiPath = Parameters.getHostEnv()+apiPath;
        if (!headers.isEmpty()) {
            request.headers(headers);
        }
        if (!cookies.isEmpty()) {
            request.cookies(cookies);
        }
        if (Parameters.getContentType().equals(ContentType.JSON)) {
            if (!params.isEmpty()) {
                request.queryParams(params);
            }
        }
        if (Parameters.getContentType().equals(ContentType.FILE)) {
            if (!params.isEmpty()) {
                List<String> keys = new ArrayList<>();
                params.forEach((key, value) -> {
                    try {
                        //上传文件参数名称判断，根据接口参数变动 todo
                        if (value != null && (key.equalsIgnoreCase("files") || key.equalsIgnoreCase("file"))) {
                            String path = ApiKit.class.getClassLoader().getResource("upload/").getPath().substring(1);
                            //判断是否linux：linux系统目录加根目录斜杠
                            if (!FsKit.isWindows()) {
                                path = "/" + path;
                            }
                            String[] arrayValue = value.toString().split(",");
                            for (String pStr : arrayValue) {
                                //一次上传多个文件，阈值500个，上传一百次,一次5个文件 todo
//                                for(int i = 0; i <= 100;i++){
                                request.multiPart(key, new File(path + pStr), FsKit.getMimeType(path + pStr));
//                                }
//                                System.out.println("文件上传总个数"+5*100);
                            }
                            keys.add(key);
                        }
                    } catch (Exception e) {
                        params.put(key, value);
                        e.getMessage();
                    }
                });
                //移除文件参数 根据具体接口改动
//                keys.forEach(k -> {
//                    params.remove(k);
//                });
                request.queryParams(params);
            }
        }

        if (!body.isEmpty()) {
            request.body(body);
        }
        request.when().log().all();
        Response response;
        switch (method.toUpperCase()) {
            case "GET":
                response = request.get(apiPath.trim());
                break;
            case "POST":
                response = request.post(apiPath.trim());
                break;
            case "PUT":
                response = request.put(apiPath.trim());
                break;
            case "DELETE":
                response = request.delete(apiPath.trim());
                break;
            default:
                response = request.get(apiPath.trim());
                break;
        }
        response.prettyPrint();
        return response;
    }

    public static Map<String,Object> parseAttr(ArrayList<Attributes> attrs){
        Map<String,Object> params = new HashMap<>();
        attrs.forEach( attr->{
            params.put(attr.getName(),attr.getData());
        });
        return params;
    }
    public static String jsonPath(Response response,String path){
        Object o = response.jsonPath().get(path);
       return String.valueOf(o);
    }
}
